﻿using System.Globalization;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;


namespace ProxyTool.Services.Utils
{
    /// <summary>
    ///     Json 帮助类
    /// </summary>
    public static class JsonHelper
    {
        private const string DefaultDateTimeFormat = "yyyy-MM-dd HH:mm:ss";
        private const Formatting DefaulResponeJsonFormatting = Formatting.None;

        /// <summary>
        ///     获取默认Json序列化配置
        /// </summary>
        /// <returns></returns>
        public static JsonSerializerSettings GetDefaultJsonSerializerSettings()
        {
            return GetJsonSerializerSettings(new CamelCasePropertyNamesContractResolver());
        }

        /// <summary>
        ///     获取Json序列化配置
        /// </summary>
        /// <returns></returns>
        public static JsonSerializerSettings GetJsonSerializerSettings(IContractResolver resolver)
        {
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.ContractResolver = resolver;
            settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            settings.Converters.Add(new IsoDateTimeConverter
            {
                DateTimeFormat = DefaultDateTimeFormat,
                DateTimeStyles = DateTimeStyles.AdjustToUniversal
            });
            return settings;
        }

        /// <summary>
        ///     获取默认JsonSerializer
        /// </summary>
        /// <returns></returns>
        public static JsonSerializer GetDefaultJsonSerializer()
        {
            return GetJsonSerializer(new CamelCasePropertyNamesContractResolver());
        }

        /// <summary>
        ///     获取JsonSerializer
        /// </summary>
        /// <param name="resolver"></param>
        /// <returns></returns>
        public static JsonSerializer GetJsonSerializer(IContractResolver resolver)
        {
            JsonSerializer serializer = new JsonSerializer();
            serializer.ContractResolver = resolver;
            serializer.MissingMemberHandling = MissingMemberHandling.Ignore;
            serializer.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            string dateTimeFormat = DefaultDateTimeFormat;
            serializer.Converters.Add(new IsoDateTimeConverter
            {
                DateTimeFormat = string.IsNullOrEmpty(dateTimeFormat) ? DefaultDateTimeFormat : dateTimeFormat,
                DateTimeStyles = DateTimeStyles.AdjustToUniversal
            });
            return serializer;
        }

        /// <summary>
        ///     对象转为Json
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="formatting"></param>
        /// <returns></returns>
        public static string ObjectToJsonString(object obj, Formatting formatting = Formatting.Indented)
        {
            if (obj == null) return string.Empty;
            var settings = GetDefaultJsonSerializerSettings();
            return JsonConvert.SerializeObject(obj, formatting, settings);
        }

        /// <summary>
        ///     Json字符串转为对象
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public static T ObjectFromJsonString<T>(string jsonString)
        {
            if (string.IsNullOrEmpty(jsonString)) return default(T);
            JsonSerializerSettings settings = GetDefaultJsonSerializerSettings();
            return JsonConvert.DeserializeObject<T>(jsonString, settings);
        }
    }
}